package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: HuangFu
 * @Date: 2020/2/14 22:08
 * @Description:
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private TokenDecode tokenDecode;

    /**
     * 添加购物车
     * @param skuId
     * @param num
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestParam("skuId") String skuId,@RequestParam("num") Integer num){
        String username = tokenDecode.getUserInfo().get("username");
        cartService.add(skuId,num,username);
        return new Result(true, StatusCode.OK,"添加购物车成功");
    }

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public Result<Map> list(){
        String username = tokenDecode.getUserInfo().get("username");
        Map cartMap = cartService.list(username);
        return new Result(true, StatusCode.OK,"查询购物车成功",cartMap);
    }

}
